﻿using FAMailUtils;
using FAMailUtils.Data.DAO;
using FAMailUtils.Data.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineBusinesses.Areas.CustomerAdmin
{
    public partial class ViewGroups : System.Web.UI.Page
    {
        DepartmentDAO dao = new DepartmentDAO();
        AccountDAO dao2 = new AccountDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                int id = 0;
                int.TryParse(Request.QueryString["id"], out id);
                if (id > 0)
                {
                    DepartmentDTO department = dao.GetByDepartmentID(id, ((AdminCustomer_Master)Page.Master).getUserLogin().Client.UserID);
                    if (department.DepartmentID > 0)
                    {
                        hdDepartmentID.Value = department.DepartmentID.ToString();
                        lbDepartmentName.Text = department.DepartmentName;
                        lbDepartmentDesc.Text = department.Description;
                        lvDepartment.DataSource = dao.GetAccountByDepartmentID(department.DepartmentID, department.UserID);
                        lvDepartment.DataBind();
                    }
                }
            }
            catch (Exception ex)
            {
                Common.logs(Request.RawUrl + "-" + Common.GetCurrentMethod(), ex, Common.logType.error);
            }
        }

        protected void btnDel_Click(object sender, CommandEventArgs e)
        {
            try
            {
                int accountID = Convert.ToInt32(e.CommandArgument);
                dao.RemoveAccountFromDepartment(accountID);
                Response.Redirect(Request.RawUrl);
            }
            catch (Exception ex)
            {
                Common.logs(Request.RawUrl + "-" + Common.GetCurrentMethod(), ex, Common.logType.error);
            }
        }

        protected void btnMainDepartment_Click(object sender, CommandEventArgs e)
        {
            try
            {
                int accountID = Convert.ToInt32(e.CommandArgument);
                AccountDTO account = dao2.GetByAccountID(accountID, ((AdminCustomer_Master)Page.Master).getUserLogin().Client.ClientID);
                dao2.SetMainDepartment(account);
                Response.Redirect(Request.RawUrl);
            }
            catch (Exception ex)
            {
                Common.logs(Request.RawUrl + "-" + Common.GetCurrentMethod(), ex, Common.logType.error);
            }
        }
    }
}